package com.aznest.controller;

import com.aznest.result.Result;
import com.aznest.dto.OrdersDTO;
import com.aznest.entity.Orders;
import com.aznest.service.OrdersService;
import com.aznest.vo.OrdersVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.DocFlavor;
import java.util.List;

/**
 * 订单信息表前端操作接口
 **/
@RestController
@RequestMapping("/orders")
@Api(tags = "订单相关接口")
@Slf4j
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    /**
     * 添加订单
     */
    @PostMapping("/add")
    @ApiOperation("添加订单")
    public Result add(@RequestBody OrdersDTO orders) {
        String orderId = ordersService.add(orders);
        return Result.success(orderId);
    }

    /**
     * 根据ID删除订单
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除订单")
    public Result deleteById(@PathVariable Integer id) {
        ordersService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除订单
     */
    @DeleteMapping("/delete/batch")
    @ApiOperation("批量删除订单")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改订单信息
     */
    @PutMapping("/update")
    @ApiOperation("修改订单信息")
    public Result updateById(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return Result.success();
    }

    /**
     * 根据ID查询订单信息
     */
    @GetMapping("/selectById/{id}")
    @ApiOperation("根据ID查询订单信息")
    public Result selectById(@PathVariable Integer id) {
        Orders orders = ordersService.selectById(id);
        return Result.success(orders);
    }

    /**
     * 根据用户ID查询订单信息
     * @param id
     * @return
     */
    @GetMapping("/selectByUserId")
    @ApiOperation("根据用户ID查询订单信息")
    public Result selectByUserId(@RequestParam Integer id) {
        List<OrdersVO> orders = ordersService.selectByUserId(id);
        return Result.success(orders);
    }

    /**
     * 添加订单
     */
    @GetMapping("/selectAll")
    @ApiOperation("添加订单")
    public Result selectAll(Orders orders ) {
        List<OrdersVO> list = ordersService.selectAll(orders);
        return Result.success(list);
    }

    /**
     * 分页查询订单
     */
    @GetMapping("/selectPage")
    @ApiOperation("分页查询订单")
    public Result selectPage(Orders orders,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<OrdersVO> page = ordersService.selectPage(orders, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询待入住的订单
     * @param id
     * @return
     */
    @GetMapping("/selectChecking")
    @ApiOperation("查询待入住的订单")
    public Result selectChecking(@RequestParam Integer id) {
        List<OrdersVO> orders = ordersService.selectByCheckingAndHotelId(id);
        return Result.success(orders);
    }

    /**
     * 根据酒店ID查询订单
     * @param orderId
     * @return
     */
    @GetMapping("/selectByHotelId/{orderId}")
    @ApiOperation("根据酒店ID查询订单")
    public Result selectByHotelId(@PathVariable String orderId) {Orders orders = ordersService.selectByHotelId(orderId);
        return Result.success(orders);
    }
    /**
     * 修改订单状态为已支付
     * @param orderId
     * @return
     */
    @PutMapping("/paid/{orderId}")
    @ApiOperation("支付订单")
    public Result paid(@PathVariable String orderId) {
        ordersService.paid(orderId);
        return Result.success();
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PutMapping("/cancel/{orderId}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable String orderId) {
        ordersService.cancel(orderId);
        return Result.success();
    }

}
